<template>
    <div class="todayAll" v-if="isLoaded">
      <div class="today">
        <div class="today-title">
          <div class="today-recommen">
            <i class="iconfont icon-bankexingxing"></i>
            <span>今日推荐</span>
          </div>
          <div class="todayDate">
            <el-icon>
              <Calendar />
            </el-icon>
            <span>{{ todayDate.slice(0, 4) }}年{{ todayDate.slice(5, 7) }}月{{ todayDate.slice(8, 10) }}日</span>
          </div>
        </div>
        <div class="today-article">
          <div class="article-title">
            <i class="iconfont icon-qianyinhao"></i>
            <span>《{{ today.title }}》</span>
          </div>
          <div class="article-author">
            <i class="iconfont icon-zuozhe"></i>
            <span>作者：{{ today.author }}</span>
          </div>
          <div class="article-content">
            {{ today.content }}
          </div>
          <router-link v-if="today?.id" 
            :to="{ name: 'details', params: { id: today.id }}">
            <el-button class="bt" type="primary" color="#7B68EE">阅读全文</el-button>
          </router-link>
        </div>
      </div>
      <div class="before">
  <div class="before-recommen">
    <i class="iconfont icon-playback"></i>
    <span>往日推荐</span>
  </div>
  

  <div v-if="before?.length">

    <router-link 
      v-for="(item, index) in before" 
      :key="index"
      :to="{ name: 'details', params: { id: item.id }}"
    >
      <div class="before-article">
        <span class="title">{{ item.title }}</span>
        <span class="author">作者：{{ item.author }}</span>
        <span class="date">{{ item.created_at.slice(0,9) }}</span>
      </div>
    </router-link>
  </div>
  
  <div v-else class="empty-tips">
    暂无往期推荐内容
  </div>
</div>
    </div>

    <div v-else class="loading">
      <el-icon :size="20" class="is-loading">
        <Loading />
      </el-icon>
      <span>正在加载推荐内容...</span>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue'
  import { Calendar, Loading } from '@element-plus/icons-vue'
  import { formateDate } from '@/utils/formateDate.js'
  import { getNewsArticleList } from '@/api/index.js'
  import { useArticleStore } from '@/stores/articleStore'
  
  const articleStore = useArticleStore()
  const todayDate = ref('')
  const data = ref([])
  const today = ref(null) // 初始化为 null 明确表示未加载
  const before = ref(null)
  const isLoaded = ref(false) // 加载状态控制
  
  const getData = async () => {
    try {
      const res = await getNewsArticleList()
      data.value = res.data
  
      // 防御性编程：确保数据有效
      if (data.value?.length > 0) {
        today.value = data.value[0]
        before.value = data.value.slice(1, 4)
        isLoaded.value = true // 数据就绪后允许渲染
  
        // 更新 Store
        articleStore.setArticles({
          data: data.value,
          today: today.value,
          before: before.value
        })
      }
    } catch (error) {
      console.error('数据加载失败:', error)
    }
  }
  
  onMounted(async () => {
    todayDate.value = formateDate(new Date())
    await getData()
  })
  </script>

<style lang="less" scoped>
.todayAll{
    background-color:#fbf9f9;
    min-height: calc(100vh - 184px);
    padding: 16px;
    color: #7B68EE;
    overflow: auto;
    .today{
        background: #FFFFFF rgba(0,0,0,0.001);
        box-shadow: 0px 1px 2px 0px rgba(0,0,0,0.05);
        border-radius: 12px 12px 12px 12px;
        margin-bottom: 20px;
        padding: 16px;
        overflow: hidden;
        margin-bottom:16px ;
        border: 1px solid #e6e8eb;
        .today-title{
            flex-direction: column;
            display: flex;
            justify-content: center;
            background:rgba(123,104,238,0.1);
            border-radius: 12px 12px 12px 12px;
            padding: 16px;
            box-sizing: border-box;
            margin-bottom: 24px;
          .recommen{
                font-weight: bold;
                margin-right: 10px;
                font-size: 20px;
                margin-bottom: 8px;
                i{
                    font-size: 20px;
                    color: #7B68EE;
                }
                span{
                    font-weight: 600;
                    color: #000000;
                    line-height: 28px;
                    text-align: left;
                    margin-left: 14px;
                }
             }
            
            .todayDate{
                color: #7B68EE;
                i{
                    font-size: 14px;        
                }
                span{
                    font-weight: 400;
                    font-size: 14px;
                    color: #4B5563;
                    line-height: 20px;
                    text-align: left;
                    margin-left: 8px;
                }
             }
        }
       .today-article{
         flex-direction: column;
         display: flex;
         justify-content: center;
         .article-title{
            margin-bottom: 8px;
            i{
                font-size: 14px;
                margin-right: 8px;
            }
            span{
                font-weight: 600;
                font-size: 18px;
                color: #000000;
                line-height: 28px;
                text-align: left;
            }
           
         }
         .article-author{
            margin-bottom: 8px;
            i{
                font-size: 15px;
                margin-right: 8px;
            }
            span{
                font-weight: 400;
                font-size: 14px;
                color: #4B5563;
                line-height: 21px;
                text-align: left;
            }
         }
         .article-content{
            margin-bottom: 24px;
            color: #6B7280;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
            overflow: hidden;
            font-family: Roboto, Roboto;
            font-weight: 400;
            font-size: 14px;
            color: #6B7280;
            line-height: 23px;
            text-align: left;
            font-style: normal;
            text-transform: none;
            }
         .bt{
            color:#fff;
            width: 100%;
            display: block;
            height: 45px;
            border-radius: 4px 4px 4px 4px;
         }
       }
    }
    .before{
        background: #FFFFFF rgba(0,0,0,0.001);
        box-shadow: 0px 1px 2px 0px rgba(0,0,0,0.05);
        border-radius: 12px 12px 12px 12px;
        margin-bottom: 20px;
        padding: 16px;
        overflow: hidden;
        box-sizing: border-box;
        border: 1px solid #e6e8eb;
        .before-recommen{
            margin-bottom: 16px;
            i{
                color: #7B68EE;
                font-size: 14px;
                margin-right: 8px;
            }
            span{
                font-weight: 500;
                font-size: 14px;
                color: #374151;
                line-height: 21px;
                text-align: left;
            }
        }
        .before-article{
            display: flex;
            flex-direction: column;
            justify-content: center;
            margin-bottom: 16px;
            border-radius: 12px 12px 12px 12px;
            border: 1px solid #e6e8eb;
            padding:17px ;
            overflow: hidden;
            box-sizing: border-box;
           .title{
            font-weight: 500;
            font-size: 14px;
            color: #000000;
            line-height: 21px;
            text-align: left;
            margin-bottom: 4px;
           }
           .author{
            font-weight: 400;
            font-size: 14px;
            color: #6B7280;
            line-height: 20px;
            text-align: left;
            margin-bottom: 4px;
           }
           .date{
            font-weight: 400;
            font-size: 12px;
            color: #9CA3AF;
            line-height: 16px;
            text-align: left;
            margin-top: 4px;
           }
        }
    }
}
</style>